package com.goldwind.login.controller;


import com.goldwind.login.service.EmailLoginService;
import com.goldwind.pojo.LoginInfo;
import com.goldwind.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;

@Slf4j // 用于输出日志
@Controller
@RequestMapping("logins")
public class LoginController {

    /**
     * 注入redisTemplate用于缓存数据
     */
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 注入loginService用于调用其中自定义的邮件发送方法
     */
    @Autowired
    private EmailLoginService emailLoginService;

    /**
     * 发送邮件
     * @param emailAddress 目标邮件地址，作为redis存储的key
     */
    @GetMapping
    @ResponseBody
    public void sendEmail( String emailAddress){
        System.out.println("sendEmailAddress:"+emailAddress);
        log.info("====>请求参数邮箱地址为{}",emailAddress);
        // 1.生成4位随机验证码
        // 截取当前时间戳的后四位模拟验证码
        long timestamp = System.currentTimeMillis();
        String verificationCode = Long.toString(timestamp).substring(Long.toString(timestamp).length() - 4);

        log.info("====>验证码为:{}",verificationCode);
        // 2.存入redis
        // 将传入邮箱地址为key，验证码为value，存入redis中，并设置超时时间为5分钟
        redisTemplate.opsForValue().set(emailAddress,verificationCode,1, TimeUnit.MINUTES);

        // 3.编写邮件内容模板
        String emailInfo = "验证码为：" + verificationCode +"，5分钟内有效。";

        // 4.发送验证码到目标邮箱
        emailLoginService.sendEmail("3520174818@qq.com", emailAddress, "AVR验证码", emailInfo);
    }

    /**
     * 登录验证
     * @param loginInfo 接收前端传入的邮箱地址和code
     * @return 返回执行结果
     */
    @ResponseBody
    @PostMapping("check")
    public String CheckEmailCode(@RequestBody LoginInfo loginInfo){
        log.info("=====>POST请求接收到的参数:{}***{}",loginInfo.getEmailAddress(), loginInfo.getCode());

        // 取出传入数据对比redis中数据，判断是否通过登录验证
        // 判断传入的邮箱地址是否存在redis中
        if(redisTemplate.hasKey(loginInfo.getEmailAddress())){
            // 取出对应邮箱地址为key的value值
            String storyCode = redisTemplate.opsForValue().get(loginInfo.getEmailAddress());
            // 判断存在redis中的验证码和传入验证码是否相同，相同返回true
            return loginInfo.getCode().equals(storyCode)? "true":"false";
            //return loginInfo.getCode().equals("9999")? "true":"false";
        }
        return "false";
    }

    @GetMapping("1")
    public String home(String emailAddress,HttpSession session) throws Exception{
        if(emailLoginService.isEmailExist(emailAddress)){
            session.setAttribute("email",emailAddress);
            InetAddress localHost = InetAddress.getLocalHost();
            String str = localHost.getHostAddress();
            return "redirect:http://" + str + ":8080/AVR/User/home?email="+emailAddress;
            //return "redirect:http://localhost:8080/AVR/User/home?email="+emailAddress;
        }
        return "Login/accountLogin";
    }

    @ResponseBody
    @PostMapping("1")
    public String register(@RequestBody User user,HttpSession session) throws Exception {
        if(emailLoginService.insertUser(user) == 1){
            session.setAttribute("email",user.getEmail());
            return "true";
        }
        return "false";
    }

}

